package com.zaoniao.enstudy.infrastructure.persistence;

import com.zaoniao.enstudy.domain.entity.ExamAnswer;
import com.zaoniao.enstudy.domain.entity.ExamPaper;
import com.zaoniao.enstudy.domain.entity.ExamWords;
import com.zaoniao.enstudy.domain.repository.ExamRepository;
import com.zaoniao.enstudy.infrastructure.convert.ExamAnswerCrt;
import com.zaoniao.enstudy.infrastructure.convert.ExamPaperCrt;
import com.zaoniao.enstudy.infrastructure.convert.ExamWordsCrt;
import com.zaoniao.enstudy.infrastructure.mapper.ExamAnswerMapper;
import com.zaoniao.enstudy.infrastructure.mapper.ExamPaperMapper;
import com.zaoniao.enstudy.infrastructure.mapper.ExamWordsMapper;
import com.zaoniao.enstudy.infrastructure.po.ExamAnswerPo;
import com.zaoniao.enstudy.infrastructure.po.ExamPaperPo;
import com.zaoniao.enstudy.infrastructure.po.ExamWordsPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 单词测试数据持久层实现类.
 * z_exam_words 表
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExamRepositoryImpl implements ExamRepository {
    /**
     * 单词测试数据持久层.
     */
    private final ExamWordsMapper examWordsMapper;
    private final ExamPaperMapper examPaperMapper;
    private final ExamAnswerMapper examAnswerMapper;

    /**
     * list 批量插入.
     *
     * @param examWordsPos -
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void insertList(List<ExamWordsPo> examWordsPos) {
        examWordsMapper.insertList(examWordsPos);
    }

    /**
     * 删除所有数据.
     */
    @Override
    public void deleteAll() {
        examWordsMapper.deleteAll();
    }

    /**
     * 随机获取{examCount}个题目，且不再用户历史考试记录中.
     *
     * @param examCount 题目数
     * @param userId    用户Id
     * @return 考题库
     */
    @Override
    public List<ExamWords> randomSelect(Integer examCount, String userId) {
        return ExamWordsCrt.INSTANCE.po2doList(examWordsMapper.randomSelect(examCount, userId));
    }

    /**
     * 插入考试记录.
     *
     * @param examPaper 考试记录
     */
    @Override
    public ExamPaper insertExamPaper(ExamPaper examPaper) {
        ExamPaperPo examPaperPo = ExamPaperCrt.INSTANCE.do2po(examPaper);
        examPaperMapper.insertUseGeneratedKeys(examPaperPo);
        return ExamPaperCrt.INSTANCE.po2do(examPaperPo);
    }

    /**
     * 批量插入考试记录.
     *
     * @param examAnswers 考试记录
     */
    @Override
    public void insertBatchExamAnswer(List<ExamAnswer> examAnswers) {
        examAnswerMapper.insertList(ExamAnswerCrt.INSTANCE.do2poList(examAnswers));
    }

    /**
     * 开始考试.
     *
     * @param paperId 考试记录Id
     */
    @Override
    public void startExamPaper(Integer paperId) {
        ExamPaperPo examPaperPo = new ExamPaperPo();
        examPaperPo.setId(paperId);
        examPaperPo.setExamStartTime(new Date());
        examPaperMapper.updateByPrimaryKeySelective(examPaperPo);
    }

    /**
     * 结束考试.
     *
     * @param paperId 考试记录Id
     */
    @Override
    public void endExamPaper(Integer paperId) {
        ExamPaperPo examPaperPo = new ExamPaperPo();
        examPaperPo.setId(paperId);
        examPaperPo.setExamEndTime(new Date());
        examPaperMapper.updateByPrimaryKeySelective(examPaperPo);
    }

    /**
     * 查询考试记录.
     *
     * @param userId 用户Id
     * @return 考试记录列表
     */
    @Override
    public List<ExamPaper> examList(String userId) {
        Example example = new Example(ExamPaperPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);        // 条件1

        List<ExamPaperPo> examPaperPos = examPaperMapper.selectByExample(example);
        return ExamPaperCrt.INSTANCE.po2doList(examPaperPos);
    }

    /**
     * 用户答题.
     *
     * @param examAnswer 用户答题信息
     */
    @Override
    public void updateAnswer(ExamAnswer examAnswer) {

        Example example = new Example(ExamAnswerPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", examAnswer.getExamPaperId());        // 条件1

        ExamAnswerPo examAnswerPo = new ExamAnswerPo();
        examAnswerPo.setUserAnswer(examAnswer.getUserAnswer());

        examAnswerMapper.updateByExampleSelective(ExamAnswerCrt.INSTANCE.do2po(examAnswer), example);

    }

    /**
     * 考题单词详情
     *
     * @param word 单词
     * @return 单词详情
     */
    @Override
    public ExamWords getExamWord(String word) {
        Example example = new Example(ExamWordsPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("word", word);        // 条件1

        ExamWordsPo examWordsPo = examWordsMapper.selectOneByExample(example);
        return ExamWordsCrt.INSTANCE.po2do(examWordsPo);
    }

    /**
     * 更新用户答题正确数.
     *
     * @param examPaperId 考试记录Id
     */
    @Override
    public void updatePaperTrueCount(Integer examPaperId) {
        ExamPaperPo examPaperPo = examPaperMapper.selectByPrimaryKey(examPaperId);
        examPaperPo.setTrueCount(examPaperPo.getTrueCount() + 1);

        examPaperMapper.updateByPrimaryKey(examPaperPo);
    }

}
